package com.fyzk.activity.lottery.shaidan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppProgressDialog;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.chongzhi.RechargeActivity;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.dialog.SureDialog;
import com.fyzk.dialog.SureDialog.OnDialogOKCancleClickListener;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.tyjc.lottery.R;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.view.TopTitleView;

public class ShaidanDetailActivity extends BaseActivity implements onTopTitleClickListener {

	String order_id = "";
	TopTitleView topView;
	TextView txt_faqiren;
	TextView txt_ticheng;
	TextView txt_lotteryName;
	TextView txt_shineAmount;// 晒单彩金
	TextView txt_selfMuti;// 自购倍数
	TextView txt_desTitle;// 方案描述标题
	TextView txt_desContent;// 方案描述内容
	TextView txt_startTime;// 开始时间
	TextView txt_endTime;// 结束时间
	TextView txt_scretType;//保密设置

	EditText edit_gendanMuti;
	Button btn_gendan;
	LinearLayout linear;
	AppProgressDialog progressDialog;
	
	private double leftMoney;
	/**
	 * 彩金不足提醒对话框
	 */
	private SureDialog leftMoneyLowDialog = null;
	/**
	 * 确认支付对话框
	 */
	private SureDialog sureBuyDialog = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_shaidan_detail);
		order_id = getIntent().getStringExtra(IntentParamUtils.ORDER_ID);
		init();
		net();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (MySharedPreferences.getInstance().getLoginStatus()) {
//			requestData(SchemaDef.ACCOUNT_MONEY);
		}
	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub
		super.init();
		txt_scretType = (TextView)findViewById(R.id.shaidan_detail_scretTye);
		topView = (TopTitleView) findViewById(R.id.shaidan_detail_topTitle);
		topView.setOnTitleClickListener(this);
		txt_faqiren = (TextView) findViewById(R.id.shaidan_detail_faqiren);
		txt_lotteryName = (TextView) findViewById(R.id.shaidan_detail_lotteryName);
		txt_desTitle = (TextView) findViewById(R.id.shaidan_detail_descriptitle);
		txt_desContent = (TextView) findViewById(R.id.shaidan_detail_descripContent);
		txt_startTime = (TextView) findViewById(R.id.shaidan_detail_startTime);
		txt_endTime = (TextView) findViewById(R.id.shaidan_detail_endTime);
		txt_ticheng = (TextView) findViewById(R.id.shaidan_detail_ticheng);
		txt_selfMuti = (TextView) findViewById(R.id.shaidan_detail_amount);
		txt_shineAmount = (TextView) findViewById(R.id.shaidan_detail_muti);
		edit_gendanMuti = (EditText) findViewById(R.id.shaidan_detail_genMuti);
		edit_gendanMuti.clearFocus();
		btn_gendan = (Button) findViewById(R.id.shaidan_detail_btn_gendan);
		linear = (LinearLayout) findViewById(R.id.shaidan_detail_contentdetail);
		btn_gendan.setOnClickListener(this);
	}

	AppProgressDialog netProgress;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
//		super.onClick(v);
//		if (v.getId() == R.id.shaidan_detail_btn_gendan) {
//			try {
//				UMengTongJi_eventIds.BuyOnClick(this, "晒单跟单", responseData.lotteryType, responseData.lotteryName);
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
//			if (gendanAble() && isFillLeftMoney()) {
//				showSureBuyDialog();
//			}
//		}
	}

	private void net() {
		if(progressDialog == null){
			progressDialog = new AppProgressDialog(this);
			progressDialog.setContent("加载中……");
		}
		progressDialog.show();
//		requestData(SchemaDef.SHAIDAN_DETAIL);
	}

//	private ShaiDanDetailResponseData responseData;
//	FundsDetailResponseData accountObj;			//账户彩金
	String tips = "";

//	@Override
//	public void onProtocalSuccess(Object paramObject) {
//		// TODO Auto-generated method stub
//		super.onProtocalSuccess(paramObject);
//		if (paramObject instanceof ShaiDanDetailResponseData) {
//			responseData = (ShaiDanDetailResponseData) paramObject;
//			CommonResult result = responseData.commonResult;
//			if (result.codeValue == 0) {
//				mHandler.sendEmptyMessage(HandlerTypeUtils.NET_SUCCESS);
//			} else {
//				mHandler.sendEmptyMessage(HandlerTypeUtils.NET_FAIL);
//			}
//		} else if (paramObject instanceof ShaidanGenDanResponseData) {
//			ShaidanGenDanResponseData responseData =  ((ShaidanGenDanResponseData) paramObject);
//			CommonResult result = responseData.commonResult;
//			if (result.codeValue == 0) {
//				mHandler.sendEmptyMessage(HandlerTypeUtils.NET_SUCCESS2);
//			} else {
//				mHandler.sendEmptyMessage(HandlerTypeUtils.NET_FAIL2);
//			}
//		}else if (paramObject instanceof FundsDetailResponseData) {
//			accountObj = (FundsDetailResponseData) paramObject;
//			CommonResult commonMsg = accountObj.commonResult;
//			if (commonMsg.codeValue == 0) {
//				mHandler.sendEmptyMessage(HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_LEFTMONEY_SUCCESS);
//			} else {
//				mHandler.sendEmptyMessage(HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_FAIL);
//				tips = commonMsg.tips;
//			}
//		}
//	}

	private void initView() {
	
//		txt_faqiren.setValue(getText(R.string.shaidan_detail_faqiren, responseData.shine_username ));
//		txt_selfMuti.setValue(getText(R.string.shaidan_detail_selfMuti, responseData.self_muti ));
//		txt_shineAmount.setValue(getText(R.string.shaidan_detail_shineAmount,responseData.shine_amount));
//		txt_lotteryName.setValue(getText(R.string.shaidan_detail_lotteryName,responseData.lotteryName));
//		txt_desTitle.setValue("方案标题：" + responseData.declarationTitle);
//		txt_desContent.setValue("方案内容：" + responseData.declarationContent);
//		txt_ticheng.setValue(getText(R.string.shaidan_detail_ticheng,responseData.percentage));
//		txt_startTime.setValue("发起时间：" + PublicMethod.formatDate(responseData.startTime));
//		txt_endTime.setValue("截至时间：" + PublicMethod.formatDate(responseData.endTime));
//
//		if(responseData.secret_type.equals(responseData.SCRETTYPE_BAOMI)){
//			txt_scretType.append("保密方案");
//		}else if(responseData.secret_type.equals(responseData.SCRETTYPE_ENDOPEN)){
//			txt_scretType.append("截至后公开");
//		}else if(responseData.secret_type.equals(responseData.SCRETTYPE_GENOPEN)){
//			txt_scretType.append("对跟单者公开");
//		}else {
//			txt_scretType.append("完全公开");
//		}
//		try {
//			//当返回isShow为true，或晒单人即为当前用户是显示方案信息
//			if(Boolean.parseBoolean(responseData.isShow) || (responseData.shine_username.equals(MySharedPreferences.getInstance().getUserName()) &&
//					MySharedPreferences.getInstance().getLoginStatus())){
//				linear.addView(RaceFactory.getRaceView(responseData.lotteryType).getRaceView(getContext(), responseData.content, responseData.races, "hm"));
//			}
//		} catch (Exception e) {
//			L.e(TAG, "解析isShow显示方案内容出错");
//			// TODO: handle exception
//		}
	}

	public Spanned getText(int redId,String target){
		Spanned sp = null;
		try {

			String sAgeFormat = getResources().getString(redId);
			String sFinalAge = String.format(sAgeFormat, target);
			sp = Html.fromHtml(sFinalAge);
		} catch (Exception e) {
			// TODO: handle exception
			sp = Html.fromHtml("");;
		}
		return sp;
	}

	@Override
	protected void parserMessage(Message msg) {
		// TODO Auto-generated method stub
//		super.parserMessage(msg);
//		progressDialog.cancel();
//		switch (msg.what) {
//		case HandlerTypeUtils.NET_SUCCESS:
//			initView();
//			break;
//		case HandlerTypeUtils.NET_SUCCESS2:
//			try {
//				UMengTongJi_eventIds.Buy(this, "晒单跟单", responseData.lotteryType, responseData.lotteryName, getMoney(), true);
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
//			netProgress.cancel();
//			AppToast.getToast().show("跟单成功");
//			finishGendan();
//			break;
//		case HandlerTypeUtils.NET_FAIL2:
//			netProgress.cancel();
//			AppToast.getToast().show("跟单失败");
//			break;
//		case HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_LEFTMONEY_SUCCESS:
//			try {
//				leftMoney = Double.parseDouble(accountObj.balance);
//			} catch (Exception e) {
//				leftMoney = 0;
//			}
//			break;
//		case HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_FAIL:
//			AppToast.getToast().show(""+tips);
//			break;
//		default:
//			break;
//		}

	}


//	@Override
	protected void request(int schemaDef) {
		// TODO Auto-generated method stub
//		super.request(schemaDef);
//		Object requestData = null;
//		switch (schemaDef) {
//		case SchemaDef.SHAIDAN_DETAIL:
//			requestData = new ShaiDanDetailRequestData();
//			((ShaiDanDetailRequestData) requestData).id = order_id;
//
//			break;
//		case SchemaDef.SHAIDAN_GENDAN:
//			requestData = new ShaidanGenDanRequestData();
//			((ShaidanGenDanRequestData) requestData).id = order_id;
//			((ShaidanGenDanRequestData) requestData).muti = getGenDanMuti();
//			((ShaidanGenDanRequestData) requestData).genAmount = getMoney()*100 + "";
//			break;
//		case SchemaDef.ACCOUNT_MONEY:
//			requestData = new FundsDetailRequestData();
//			break;
//		}
//		ProtocalEngine engine = new ProtocalEngine(this);
//		engine.request(this, schemaDef, requestData);
	}
	public static final int GENDAN_SUCCESS = 0x1111;
	
	private void finishGendan(){
		setResult(GENDAN_SUCCESS);
		finish();
	}

	private String getGenDanMuti() {
		// TODO Auto-generated method stub
		String muti = edit_gendanMuti.getText().toString();
		return muti;
	}
	private int getMoney(){
//		try {
//			return Integer.parseInt(getGenDanMuti()) * Integer.parseInt(responseData.shine_amount);
//		} catch (Exception e) {
//			// TODO: handle exception
			return 0 ;
//		}
	}

	@Override
	public void onLeftClick() {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	public void onCenterClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRightClick() {
		// TODO Auto-generated method stub

	}
	@Override
	public void onRight2Click() {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 显示彩金不足对话框
	 */
	private void showLeftMoneyLowDialog(){
		if(leftMoneyLowDialog == null){
			OnDialogOKCancleClickListener dialogOKCancleClickListener = new OnDialogOKCancleClickListener() {

				@Override
				public void onOKClick() {
					Intent intent = new Intent(getContext(), RechargeActivity.class);
					startActivity(intent);
					leftMoneyLowDialog.dismiss();
				}

				@Override
				public void onCancleClick() {
					// TODO Auto-generated method stub
					leftMoneyLowDialog.dismiss();
				}
			};
			leftMoneyLowDialog = new SureDialog(this,dialogOKCancleClickListener,"","您的彩金不足！","充值","取消");
		}
		leftMoneyLowDialog.show();
	}
	
	/**
	 * 确认支付对话框
	 */
	private void showSureBuyDialog(){


		if(sureBuyDialog == null){
			sureBuyDialog = new SureDialog(this,new OnDialogOKCancleClickListener() {

				@Override
				public void onOKClick() {
//				DJ_ProgressDialog.show(getContext(), true, true);
//				requestData(SchemaDef.HEMAI_FAQI);
					if (null == netProgress) {
						netProgress = new AppProgressDialog(ShaidanDetailActivity.this);
					}
					netProgress.show();
//					requestData(SchemaDef.SHAIDAN_GENDAN);
					sureBuyDialog.dismiss();
				}

				@Override
				public void onCancleClick() {
					// TODO Auto-generated method stub
					sureBuyDialog.dismiss();
				}
			},"您的彩金为："+leftMoney + "米","支付："+getMoney() + "米","支付","取消");
		}
		sureBuyDialog.show();
	}
}
